#include "Player.h"
#include "Bullet.h"
#include "GameScene.h"
#include "TextureLoader.h"
#include "Game.h"
#define BIRTH_X 4*MAP_BLOCK_WIDTH
#define BIRTH_Y 12*MAP_BLOCK_HEIGHT

Player::Player(GameScene* scene)
    :Tank(BIRTH_X,BIRTH_Y,MAP_BLOCK_WIDTH,MAP_BLOCK_HEIGHT,Angle::UP,0.12f,scene_),level_(0)
{
    scene_ = scene;
    isEnemy_ = false;
}

Player::~Player()
{
}

void Player::Update(float dt)
{
    auto keyBoardState =SDL_GetKeyboardState(nullptr);
    if (keyBoardState[SDL_SCANCODE_W])
    {
        angle_ = Angle::UP;
        Move(dt);
    }
    if (keyBoardState[SDL_SCANCODE_S])
    {
        angle_=Angle::DOWN;
        Move(dt);
    }
    if (keyBoardState[SDL_SCANCODE_A])
    {
        angle_ = Angle::LEFT;
        Move(dt);
    }
    if (keyBoardState[SDL_SCANCODE_D])
    {
        angle_ = Angle::RIGHT;
        Move(dt);
    }
    if(keyBoardState[SDL_SCANCODE_J])
    {
        shoot();
    }
}

void Player::Render()
{
    if(isRunning_)
    {
        isRunning_ = false;
        index_ = !index_;
    }
    auto renderer = Game::GetInstance().GetRenderer();
    auto texture=TextureLoader::Instance().GetPlayer1Texture(angle_, level_, index_);
    SDL_RenderCopy(renderer,texture.first , &texture.second, &GetRect());

}

void Player::shoot()
{
    if (bullet_.expired())
    {
        auto bullet = std::make_shared<Bullet>(x_ + width_/2 - BULLET_SIZE/2, y_ + height_/2 - BULLET_SIZE/2, BULLET_SIZE, BULLET_SIZE, angle_, 0.3f, scene_);
        bullet->isEnemyBullet_ = false;
        bullet_ = bullet;
        scene_->GetBullets().push_back(bullet_.lock());
    }
}
